//引入接口
import API from '../../config/server'

Page({
  data:{
    nav: [],
    heroList: [],
    navType: 'all',
    allList: [],
  },
  onLoad()
  {
    //生命周期钩子
    this.heroData();
  },
  //请求数据英雄分类和列表
  heroData(){
    let that = this
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: API.herolist,
      method: 'GET',
      data: '',
      dataType: 'json',
      success: (result) => {
        // console.info(result)
        const data = result.data.data;
        that.setData({
          nav: data.nav,
          heroList: data.heroList,
          allList: data.heroList,
        })
      },
      fail: (res) => {
       wx.showToast({
         title: '网络错误',
         icon: 'error',
       })
      },
      complete: (res) => {
        wx.hideLoading()
      },
    })
  },
  changeNav(e){
    let type = e.currentTarget.dataset.type
    
    let list = this.data.allList
    let newList = list.filter((hero) => {
        return hero.camptype == type;
    });

    if(type == 'all'){
      newList = list
    }

    this.setData({
      navType: type,
      heroList: newList
    });

  },
  //跳转到详细页
  toDetail(e){
    wx.showLoading({
      title: '加载中...',
    })
    let heroid = e.currentTarget.dataset.heroid,
        infourl = e.currentTarget.dataset.infourl;
    wx.navigateTo({
      url: `/pages/heroDetail/heroDetail?id=${heroid}&url=${infourl}`,
      success: (result) => {
        
      },
      fail: (res) => {
        wx.showToast({
          title: '网络错误',
          icon: 'error',
        })
       },
       complete: (res) => {
         wx.hideLoading()
       },
    })
  },
})